package com.andryu.opengl.fragment

import android.opengl.GLSurfaceView
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.andryu.base.LogUtils
import com.andryu.base.constant.CommonConstants
import com.andryu.base.entity.LearnListEntity
import com.andryu.base.ui.fragment.BaseFragment
import com.andryu.opengl.java.GraphicRectangleImpl
import com.andryu.opengl.jni.LabelSimpleHelper
import com.andryu.opengl.render.LabelSimpleRender
import com.andryu.opengl.view.OpenGLSurfaceView

class OpenglSurfaceFragment: BaseFragment() {

    private lateinit var mSurfaceView: OpenGLSurfaceView
    private lateinit var mRender: GLSurfaceView.Renderer
    private var entity:LearnListEntity ?= null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        entity = arguments?.getParcelable(CommonConstants.NAVI_FRAGMENT_LEARN_ENTITY)
        LogUtils.d("OpenglSurface--$entity")
        mRender = entity?.let {
            if (it.isCpp) {
                LabelSimpleRender(true,it.graphicType)
            } else {
                it.javaObserver?.let { it1 -> LabelSimpleRender(false,it.graphicType, it1) }
            }
        } ?: run {
            // 如果 entity 为空，则可以在这里提供默认的 mRender 实例
            LabelSimpleRender(true, 1)
        }
        mSurfaceView = OpenGLSurfaceView(requireContext(), mRender)
        return mSurfaceView
    }

    override fun onDestroyView() {
        super.onDestroyView()
        entity?.let {
            if (it.isCpp){
                LabelSimpleHelper.destroy()
            }
        }
    }
}